// routes/cards/findAllByGroup.js
const express = require('express');
const pool = require('../../config/pool'); // 使用连接池
const router = express.Router();
const isAdmin = require('../../middleware/isAdmin');

// 创建卡片 只有管理员才可以
router.post('/', isAdmin, (req, res) => {
    const { type_id, image_url, face_value, redemption_discount } = req.body;
    // 校验type_id、image_url、face_value、redemption_discount是否存在
    if (!type_id || !image_url || !face_value || !redemption_discount) {
        return res.status(400).json({
            code: 0,
            message: '卡片类型id、图片、面值和兑换折扣不能为空',
            success: false
        });
    }
    const sql = 'INSERT INTO cards (card_type_id, image_url, face_value, redemption_discount) VALUES (?, ?, ?, ?)';
    pool.query(sql, [type_id, image_url, face_value, redemption_discount], (err, result) => {
        if (err) {
            res.status(500).json({
                code: 0,
                message: err.message,
                success: false
            });
        }
        res.status(200).json({
            code: 1,
            message: '卡片创建成功',
            success: true
        });

    });
});

module.exports = router;